package com.jiantong.web.controller.news;

import com.jiantong.news.model.ThinktankVideo;
import com.jiantong.news.service.ThinktankVideoReadService;
import com.jiantong.news.service.ThinktankVideoWriteService;
import io.terminus.boot.rpc.common.annotation.RpcConsumer;
import io.terminus.common.model.Paging;
import io.terminus.common.model.Response;
import io.terminus.pampas.openplatform.exceptions.OPClientException;
import io.terminus.pampas.openplatform.exceptions.OPServerException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

/**
 * Created by mz on 9/5/17.
 *
 */
@RestController
@Slf4j
public class ThinktankVideoController {

    @RpcConsumer
    private ThinktankVideoReadService thinktankVideoReadService;

    @RpcConsumer
    private ThinktankVideoWriteService thinktankVideoWriteService;

    @PostMapping(value = "/api/create/think/tank/video")
    public Long createThinkTankVideo(@RequestBody ThinktankVideo thinktankVideo){
        if(thinktankVideo.getId() != null){
            log.error("this thinktankVideo is exist");
            throw new OPClientException("this thinktankVideo is exist");
        }
        Response<Long> response = thinktankVideoWriteService.create(thinktankVideo);
        if(!response.isSuccess()){
            log.error("failed to create think tank video case = {}",response.getError());
            throw new OPServerException(response.getError());
        }
        return response.getResult();
    }

    @PutMapping(value = "/api/update/think/tank/video")
    public Boolean updateThinkTankVideo(@RequestBody ThinktankVideo thinktankVideo){
        if(thinktankVideo.getId() ==  null){
            log.error("this thinktankVideo'id is null");
            throw new OPClientException("this thinktankVideo'id is null");
        }
        Response<Boolean> response = thinktankVideoWriteService.update(thinktankVideo);
        if(!response.isSuccess()){
            log.error("failed to update think tank video case = {}",response.getError());
            throw new OPServerException(response.getError());
        }
        return response.getResult();
    }

    @GetMapping(value = "/api/paging/think/tank/video")
    public Paging<ThinktankVideo> thinkTankVideoPaging(@RequestParam Integer pageNo,
                                                       @RequestParam Integer pageSize,
                                                       @RequestParam Integer type){
        Response<Paging<ThinktankVideo>> response = thinktankVideoReadService.pagingThinktankVideo(pageNo,pageSize,type);
        if(!response.isSuccess()){
            log.error("failed to paging think tank video type = {} ,case = {}",response.getError());
            throw new OPServerException(response.getError());
        }
        return response.getResult();
    }

    @GetMapping(value = "/api/find/think/tank/video/by/id")
    public ThinktankVideo findThinkTankVideoById(@RequestParam Long id){
        if (id == null){
            log.error("this id is null");
            throw new OPClientException("this id is null");
        }
        Response<ThinktankVideo> response = thinktankVideoReadService.finById(id);
        if(!response.isSuccess()){
            log.error("failed to find think tank video by id = {} ,case = {}",id,response.getError());
            throw new OPServerException(response.getError());
        }
        return response.getResult();
    }
}
